Skip to content

gh-128074: Support +HH format as time zone for strptime#130390

Open
donbarbos wants to merge 14 commits intopython:mainfrom
donbarbos:issue-128074
Open

gh-128074: Support +HH format as time zone for strptime#130390
donbarbos wants to merge 14 commits intopython:mainfrom
donbarbos:issue-128074

Conversation

@donbarbos
Copy link
Contributor

@donbarbos donbarbos commented Feb 21, 2025

I made the minute (MM) part optional for time zone.

New behavior:

>>> from datetime import datetime
>>> datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
datetime.datetime(2023, 5, 25, 15, 35, 5, 666000, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600)))

Old behavior:

>>> from datetime import datetime
>>> datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../cpython/Lib/_strptime.py", line 700, in _strptime_datetime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File ".../cpython/Lib/_strptime.py", line 453, in _strptime
    raise ValueError("time data %r does not match format %r" %
                     (data_string, format))
ValueError: time data '2023-05-25T15:35:05.666+11' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

📚 Documentation preview 📚: https://cpython-previews--130390.org.readthedocs.build/en/130390/library/datetime.html

Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has conflicts now.

@donbarbos
Copy link
Contributor Author

After adding the %:z directive, I merged the main branch here and changed the regex for %:z so that it could also only read HH format
cc @pganssle I think this PR could be reviewed after adding %:z directive

@donbarbos donbarbos changed the title gh-128074: Add support +HH format as time zone in datetime.strptime gh-128074: Support +HH format as time zone for strptime Sep 19, 2025
Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the conflicts.

Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has side effects, e.g. on main:

>>> datetime.strptime("+1020", "%z%M")
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    datetime.strptime("+1020", "%z%M")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/stan/dev/cpython/Lib/_strptime.py", line 830, in _strptime_datetime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/home/stan/dev/cpython/Lib/_strptime.py", line 560, in _strptime
    raise ValueError("time data %r does not match format %r" %
                     (data_string, format))
ValueError: time data '+1020' does not match format '%z%M'

Whereas the PR:

>>> from datetime import datetime
>>> datetime.strptime("+1020", "%z%M")
datetime.datetime(1900, 1, 1, 0, 20, tzinfo=datetime.timezone(datetime.timedelta(seconds=36000)))

We need to consider this carefully, although this is probably just a theoretical issue, I doubt it appears in practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants